<?php

/**
 * This is the model class for table "acticle_categories".
 *
 * The followings are the available columns in table 'acticle_categories':
 * @property integer $id
 * @property string $name
 * @property string $alias
 * @property string $desciption
 * @property integer $created
 */
class ActicleCategories extends PIActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ActicleCategories the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'acticle_categories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, created', 'required', 'message'=>'{attribute} Không được trống'),
			array('created, parent', 'numerical', 'integerOnly'=>true),
			array('name, alias, description', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, alias, description, created, parent', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'acticles' => array(self::HAS_MANY, 'Acticles', 'acticle_category_id', 'limit'=>6)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên danh mục',
			'alias' => 'Alias',
			'description' => 'Mô tả',
			'created' => 'Ngày tạo',
			'parent' => 'Parent',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('description',$this->desciption,true);
		$criteria->compare('created',$this->created);
		$criteria->compare('parent',$this->created);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getParent(){
		$model = self::model()->findByPk($this->parent);
		return !empty($model->name) ? $model->name : 'Danh mục chính';
	}
	public function getLink($full = false){
		if($full == true){
			return PIUrl::createAbsoluteUrl('/acticle/index',array('alias'=>$this->alias));
		}else{
			return PIUrl::createUrl('/acticle/index',array('alias'=>$this->alias));
		}
	}
}